//
//  FirstLaunchViewController.m
//  MyMovie
//
//  Created by apple on 16/5/20.
//  Copyright © 2016年 ljp. All rights reserved.
//

#import "FirstLaunchViewController.h"
#import "CustomTabBarController.h"

@interface FirstLaunchViewController ()<UIScrollViewDelegate>
{
    UIImageView *pageImageView;
    UIButton *jumpToMainButton;
}
@end

@implementation FirstLaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createSubviews];
}

- (void)createSubviews {


    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:scrollView];


    for (int i = 1; i <= 5; i++) {
        NSString *imageName = [NSString stringWithFormat:@"guide%d", i];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

        imageView.frame = CGRectMake(KScreenWidth*(i-1), 0, KScreenWidth, KScreenHeight);
        [scrollView addSubview:imageView];

    }
    scrollView.contentSize = CGSizeMake(5*KScreenWidth, 0);
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.delegate = self;

    pageImageView = [[UIImageView alloc] initWithFrame:CGRectMake((KScreenWidth-86.5)/2, KScreenHeight-50, 86.5, 13)];
    pageImageView.image = [UIImage imageNamed:@"guideProgress1"];
    [self.view addSubview:pageImageView];

    jumpToMainButton = [UIButton buttonWithType:UIButtonTypeCustom];
    jumpToMainButton.frame = CGRectMake((KScreenWidth-100)/2, KScreenHeight-100, 100, 50);
    UIColor *color = [UIColor colorWithRed:164/255.0 green:73/255.0 blue:75/255.0 alpha:1];

    [jumpToMainButton setTitle:@"进入程序" forState:UIControlStateNormal];
    [jumpToMainButton setTitleColor:color forState:UIControlStateNormal];
    [jumpToMainButton addTarget:self action:@selector(jumpToMain) forControlEvents:UIControlEventTouchUpInside];
    jumpToMainButton.hidden = YES;

    [self.view addSubview:jumpToMainButton];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGFloat xOffset = scrollView.contentOffset.x + 0.5*KScreenWidth;
    NSInteger index = xOffset/KScreenWidth;
    pageImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guideProgress%ld", index+1 ]];

    jumpToMainButton.hidden = index == 4 ? NO : YES;

}


- (void)jumpToMain {

    NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];

    NSString *key = @"notFirstLaunchApp";

    [userDefaults setObject:@"LaunchApp" forKey:key];

    CustomTabBarController *custom = [[CustomTabBarController alloc] init];
    self.view.window.rootViewController = custom;
    custom.view.transform = CGAffineTransformMakeScale(0.2, 0.2);
    [UIView animateWithDuration:0.5 animations:^{
        custom.view.transform = CGAffineTransformIdentity;
    }];
}






@end
